package com.shape.sdk.collect;

import android.content.Context;

import java.io.FileReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.fizzo.hub.sdk.Fh;
import cn.fizzo.hub.sdk.entity.AntPlusInfo;
import cn.fizzo.hub.sdk.observer.NotifyNewAntsListener;

/**
 * Created by ouzhiyin on 18/01/2018.
 */

public class FizzoAntCollector implements ICollector, NotifyNewAntsListener {
  Context context;
  private Map<String, DeviceData> devicesMap;

  public FizzoAntCollector(Context context) {
    this.context = context;
    Fh.getManager().init(context);
  }
  @Override
  public void start() {
    devicesMap = new HashMap<>();
    Fh.getManager().registerNotifyNewAntsListener(this);
  }

  @Override
  public void stop() {
    Fh.getManager().unRegisterNotifyNewAntsListener(this);
    devicesMap.clear();
  }

  @Override
  public DeviceData getHeartRate(String deviceID) {
    return devicesMap.get(deviceID);
  }

  @Override
  public void notifyAnts(List<AntPlusInfo> ants) {
    for (AntPlusInfo info: ants) {
      String hubId = info.getSerialNo();
      DeviceData deviceData = devicesMap.get(hubId);
      if (deviceData == null) {
        deviceData = new DeviceData();
      }
      deviceData.hr = info.hr;
      deviceData.deviceId = info.serialNo;
      deviceData.cadence = info.cadence;
      deviceData.rssi = info.rssi;
      deviceData.step = info.step;
      devicesMap.put(hubId, deviceData);
    }
  }
}
